package com.yztctech.a25_03_bindservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

/**
 * Created by bodhixu on 2016/9/24.
 * 贵为四大控件,却没有布局文件(.xml),只是简单的注册一下,你真挫啊,怪不得排不了四大控件之首的地位
 * 1. 没有继承IntentService,所以需要自己开启异步
 */
public class DownloadService extends Service {

    //创建触发1次
    @Override
    public void onCreate() {
        super.onCreate();
        Log.e("Tag", "DownloadService------------> onCreate");
    }

    //绑定成功后(即bindService后)触发1次;为了拿到service实例
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e("Tag", "DownloadService------------> onBind");
        MyBinder myBinder = new MyBinder();
        return myBinder;//返回一个自定义的binder
    }

    class MyBinder extends Binder {
        //获得Service对象
        public DownloadService getService() {
            Log.e("Tag", "DownloadService.MyBinder------------> getService");
            return DownloadService.this;
        }

    }

    /**
     * @desc 生命周期 对于bindService，它不会触发此方法
     */
    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.e("Tag", "DownloadService------------> onStart");
    }

    //对于bindService，它不会触发此方法
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e("Tag", "DownloadService------------> onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * todo 注意这里要开启线程
     */
    public void download(String url) {
        Log.e("Tag", "DownloadService url == " + url);
    }

    //也可以暂停下载
    public void pauseDownload() {

    }

    //播放音频
    public void playMedia() {

    }

    //暂停音频
    public void stopMedia() {

    }


    //解除绑定触发
    @Override
    public boolean onUnbind(Intent intent) {
        Log.e("Tag", "DownloadService------------> onUnbind");
        return super.onUnbind(intent);
    }

    //销毁时触发1次
    @Override
    public void onDestroy() {
        Log.e("Tag", "DownloadService------------> onDestroy");
        super.onDestroy();
    }


}
